// pages/temp/index.js
let md5 = require('../../../utils/md5');
import WxValidate from '../../../utils/validate'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: {}, // 存储用户信息
  },
  submit(e) {
    const { oldPassword, confirmPassword, newPassword } = e.detail.value
    let that = this
    // 进行规则校检
    if (!this.WxValidate.checkForm(e.detail.value)) {
      let msg = this.WxValidate.errorList[0];
      this.showModal(msg);
      return false
    }
    // 新旧密码不一致也提示
    if (oldPassword == newPassword) {
      wx.showModal({
        content: '新密码与旧密码一致',
        showCancel: false,
      })
      return
    }
    //传递数据
    let test = this.data.user
    let obj = JSON.parse(JSON.stringify(test));
    let pbj = {};
    pbj.oldPassword = md5(oldPassword);
    pbj.password2 = md5(confirmPassword);
    pbj.password1 = md5(newPassword);
    this.axios.custom('User', "updatePassword", pbj).then((res) => {
      if (res.code == '200') {
        wx.showToast({
          title: '保存成功',
          icon: 'success',
          duration: 2000,
          success: () => {
            that.loginOut()
          }
        })
      } else {
        this.showModal(res)
      }

    })
    console.log(obj);
  },
  // 展示信息
  showModal(error) {
    wx.showModal({
      content: error.msg,
      showCancel: false,
    })
  },
  loginOut() {
    this
      .axios
      .remove('Session')
      .then(() => {
        // 先取出来保存
        let username = wx.getStorageSync('cacheName')
        this.Store.clear();
        // 再写入
        wx.setStorageSync('cacheName', username);
        this.Router.push({
          path: "login",
          openType: 'reLaunch',
        })
      })

  },
  //获取用户信息
  _getUser() {
    let id = this.Store.getItem('userid');
    if (id) {
      // 获取数据字典数据
      this.axios
        .getObject('User', id)
        .then((res) => {
          if (res.code == '200') {
            this.data.user = res.data
            console.log(this.data.user, '赋值')
          }
          console.log(res)
        })
    }
  },
  initValidate() {
    const rules = {
      oldPassword: {
        required: true,
      },
      newPassword: {
        required: true,
        minlength: 8,
        limitInput: true,
      },
      confirmPassword: {
        required: true,
      },
    }
    const messages = {
      oldPassword: {
        required: '请输入旧密码',
      },
      newPassword: {
        required: '请输入密码',
        minlength: '密码长度不能少于8位',
      },
      confirmPassword: {
        required: '请输入确认密码',
      },
    }
    let that = this;
    this.WxValidate = new WxValidate(rules, messages)
    this.WxValidate.addMethod('limitInput', (value, param) => {
      return this.WxValidate.optional(value) || /(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[^a-zA-Z0-9])/.test(value);
    }, '密码包含大小写英文数字和符号')
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 创建校检器
    this.initValidate();
    this._getUser();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})